<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav"></detail-nav-bar>
    <scroll class="content" ref="scroll" :probeType="3" @scroll="contentScroll">
      <detail-swiper :topImages="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info
        :detailInfo="detailInfo"
        @imgLoad="imgLoad"
      ></detail-goods-info>
      <detail-param-info :paramInfo="paramInfo" ref="params"></detail-param-info>
      <detail-comment-info :commentInfo="commentInfo" ref="comment"></detail-comment-info>
      <goods-list :goods='recommends' ref="recommends"></goods-list>
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>

    <!-- <toast :message="message" :show="show"></toast> -->
  </div>
</template>

<script>
import DetailNavBar from "./childDetail/DetailNavBar.vue";
import DetailSwiper from "./childDetail/DetailSwiper.vue";
import DetailBaseInfo from "./childDetail/DetailBaseInfo.vue";
import DetailShopInfo from "./childDetail/DetailShopInfo.vue";
import DetailGoodsInfo from "./childDetail/DetailGoodsInfo.vue";
import DetailParamInfo from './childDetail/DetailParamInfo.vue';
import DetailCommentInfo from './childDetail/DetailCommentInfo.vue';
import GoodsList from '@/components/content/goods/GoodsList.vue';
import DetailBottomBar from './childDetail/DetailBottomBar.vue';
import BackTop from '@/components/content/backTop/BackTop.vue';

import Scroll from "@/components/common/scroll/Scroll.vue";

import { getDetail, Goods, Shop ,GoodsParam,getRecommend} from "@/network/detail";
import {debounce} from '@/common/utils'
import {itemListenerMixin,backTop} from "@/common/mixin"

import {mapActions} from 'vuex'

// import Toast from '@/components/common/toast/Toast.vue';

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    BackTop,
    // Toast
  },
  mixins: [itemListenerMixin,backTop],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0,
      // message: "",
      // show: false
    };
  },
  created() {
    // 保存传入的iid
    this.iid = this.$route.query.iid;

    // 根据iid，请求详情数据
    getDetail(this.iid).then((res) => {
      // console.log(res);
      const data = res.result;
      // 获取顶部轮播信息
      this.topImages = data.itemInfo.topImages;

      // 获取商品基本信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );

      // 获取店铺信息
      this.shop = new Shop(data.shopInfo);

      // 获取详情页信息
      this.detailInfo = data.detailInfo;

      // 获取参数信息
      this.paramInfo = new GoodsParam(data.itemParams.info,data.itemParams.rule)

      // 获取评论信息
      if(data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0]
      }
    })

    getRecommend().then(res => {
      // console.log(res);
      this.recommends = res.data.list
    })

    this.getThemeTopY = debounce(() => {
      this.themeTopYs = []
      this.themeTopYs.push(0)
      this.themeTopYs.push(this.$refs.params.$el.offsetTop)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      this.themeTopYs.push(this.$refs.recommends.$el.offsetTop)
      this.themeTopYs.push(Number.MAX_VALUE)
    },100)
  },
  mounted() {
    // const refresh = debounce(this.$refs.scroll.refresh,100)

    // this.itemImgListener = () => {
    //   refresh()
    // }

    // this.$bus.$on('itemImageLoad', this.itemImgListener)

  },
  destroyed() {
    this.$bus.$off('itemImageLoad', this.itemImgListener)
  },
  methods: {
    ...mapActions(["addCart"]),
    imgLoad() {
      this.$refs.scroll.refresh();

      this.getThemeTopY()
    },
    titleClick(index) {
      this.$refs.scroll.scrollTo(0,-this.themeTopYs[index],200)
    },
    contentScroll(position) {
      // 记录滑动的y值
      const positionY = -position.y

      const length = this.themeTopYs.length

      // for(let i = 0; i < length; i++) { //遍历themeTopYs数组，看当前y在那个模块之间
      //   // this.currentIndex !== i 防止过于频繁的赋值
      //   // (i < length-1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i+1]) 判断0到最后之间的高度
      //   // (i === length -1 && positionY >= this.themeTopYs[i]) 判断最后高度之后的
      //   if(this.currentIndex !== i && ((i < length-1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i+1]) || (i === length -1 && positionY >= this.themeTopYs[i]))) {
      //     this.currentIndex = i
      //     // 把当前i值，传递给detail的nav模块，使其随之改变
      //     this.$refs.nav.currentIndex = this.currentIndex
      //   }
      // }

      // 加强效率做法，给themeTopYs数组加上一个MAX_VALUE，这样最后一个也不会超出高度
      for (let i = 0; i < length-1; i++) {
        if((this.currentIndex !== i) && (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i+1])) {
          this.currentIndex = i
          // 把当前i值，传递给detail的nav模块，使其随之改变
          this.$refs.nav.currentIndex = this.currentIndex
        }
      }

      // 判断backTop模块是否显示
      this.isShowBackTop = Math.abs(position.y) > 1000
    },
    addToCart() {
      // 获取商品信息
      const product = {}
      product.image = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPrice
      product.iid = this.iid

      // 将商品加入到购物车中
      // 调用vuex中的actions方法
      this.$store.dispatch('addCart',product).then(res => {
        // this.show = true
        // this.message = res

        // setTimeout(() => {
        //   this.show = false
        //   this.message = ''
        // },1500)
        // console.log(res);

        this.$toast.show(res, 1500)
      })

      // 也可以通过mapActions的方式直接调用vuex中actions中的方法
      // this.addCart(product).then(res => {
      //   this.$toast.show(res, 1500)
      //   console.log(res);
      // })
    }
  },
};
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}

.detail-nav {
    position: relative;
    z-index: 9;
    background-color: #fff;
  }

.content {
  position: absolute;
  top: 44px;
  bottom: 65px;
  overflow: hidden;
}
</style>
